package com.seed.application.context.impl;

import com.mybatisflex.core.datasource.DataSourceKey;
import com.seed.application.Sa;
import com.seed.application.context.Custom;
import com.seed.application.context.SeedContext;
import com.seed.application.env.MicroEnv;
import com.seed.core.action.Action;
import com.seed.core.configuration.MicroConfiguration;
import com.seed.core.pojo.seed.Seed;
import com.seed.core.query.App;
import com.seed.core.query.Micro;
import com.seed.manager.LibManager;
import com.seed.uaa.Uaa;
import com.seed.uaa.util.MergeUtil;
import com.seed.util.ActionsKit;
import com.seed.util.AppLoadKit;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 单体模式的主应用数据库为springboot配置的默认数据库
 * 微服务数据库为mybatis-flex配置的多数据源数据库
 */
public class SingleSeedContext implements SeedContext {

    private final App app;

    public SingleSeedContext() {
        this.app = new App(Sa.getConfiguration());

        init();
    }

    public void init() {
        List<String> libNames = app.getConfigurationLibs();
        Map<String, Seed> libSeeds = LibManager.getLibSeeds(libNames);
        libSeeds.putAll(Uaa.getSeeds());
        Map<String, Seed> seeds = MergeUtil.initSeeds(
                app.getConfigurationSeeds(),
                libSeeds,
                Custom.getSeeds()
        );

        Map<String, Action> libActions = LibManager.getLibActions(libNames);
        libActions.putAll(Uaa.getActions());
        Map<String, Action> actions = MergeUtil.initActions(
                ActionsKit.initMethods(app.getConfigurationActions()),
                libActions,
                Custom.getActions()
        );

        app.setSeeds(seeds);
        app.setActions(actions);
        Sa.getDdl().run(seeds); // 使用主库，即springboot配置文件里配置的数据库
        // TODO 初始化租户库 可直接查询租户信息

        Map<String, MicroConfiguration> microConfigurations = app.getConfigurationMicros();
        if(microConfigurations == null) return;

        Map<String, Micro> micros = new HashMap<>();
        for(Map.Entry<String, MicroConfiguration> entry: microConfigurations.entrySet()) {
            String microName = entry.getKey();
            List<String> microLibNames = app.getConfigurationLibs(microName);

            Map<String, Seed> microLibSeeds = LibManager.getLibSeeds(microLibNames);
            Map<String, Seed> microSeeds = MergeUtil.initSeeds(
                    app.getConfigurationSeeds(microName),
                    microLibSeeds,
                    Custom.getSeeds(microName)
            );

            Map<String, Action> microLibActions = LibManager.getLibActions(microLibNames);
            Map<String, Action> microActions = MergeUtil.initActions(
                    ActionsKit.initMethods(app.getConfigurationActions(microName)),
                    microLibActions,
                    Custom.getActions(microName)
            );

            micros.put(microName, new Micro(microSeeds, microActions));
            // 使用多数据源，即springboot配置文件里配置的mybatis-flex多数据源
            DataSourceKey.use(microName, () -> Sa.getDdl().run(microSeeds));
            // TODO 初始化租户库 可切换到主库，然后直接查询租户信息
        }
        app.setMicros(micros);
    }

    @Override
    public App getApp() {
        return app;
    }

    @Override
    public Micro getMicro() {
        return getApp().getMicro(MicroEnv.getMicroName());
    }
}
